<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <!-- vue3新增 -->
      <!-- 了解 -->
      <h2>自定义v-model修饰符</h2>
      <p>{{ myText }}</p>
      <my-component v-model.capitalize="myText"></my-component>
    </div>

    <script src="./Vue3.js"></script>
    <script>
      const { createApp } = Vue;

      const MyComponent = {
        // props: ["modelValue"],
        props: {
          modelValue: String,
          // 当我们添加了v-model的修饰符以后自动会有的
          modelModifiers: {
            default: () => ({}),
          },
        },
        created() {
          console.log(this.modelModifiers); // { capitalize: true }
        },
        template: `
          <input
            type="text"
            :value="modelValue"
            @input="fn"
          />
        `,
        methods: {
          fn(e) {
            let value = e.target.value;
            if (this.modelModifiers.capitalize) {
              value = value.charAt(0).toUpperCase() + value.slice(1);
            }
            this.$emit("update:model-value", value);
          },
        },
      };

      createApp({
        data() {
          return {
            myText: "abc",
          };
        },
        components: {
          MyComponent,
        },
      }).mount("#app");
    </script>
  </body>
</html>
